package com.sss.springcloud.service.impl;

import com.sss.springcloud.common.Result;
import com.sss.springcloud.service.AccountService;
import com.sss.springcloud.service.StorageService;
import io.seata.common.util.StringUtils;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.TransactionManagerHolder;
import io.seata.tm.api.GlobalTransactionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author sss , StorageService
 * @date 2020-03-14 17:24
 **/
@Component
public class AccountServiceFeignFallback implements AccountService,StorageService {
     private static final Logger log = LoggerFactory
             .getLogger(AccountServiceFeignFallback.class);
    public Result decrease(Long productId, Integer count) {
        log.info("********解决Feignfallback降级后事务不会滚问题*****");
        try {
            //TransactionManagerHolder.get().rollback(RootContext.getXID());
            if (!StringUtils.isBlank(RootContext.getXID())) GlobalTransactionContext.reload(RootContext.getXID()).rollback();
        } catch (TransactionException e) {
            log.error(e.getMessage(),e);
        }

        return Result.fail(440,"服务正在阻塞，请稍后再试(T_T)！！！");
    }

    public Result decrease(Long userId, BigDecimal money) {
        log.info("********解决Feignfallback降级后事务不会滚问题*****");
        try {
            TransactionManagerHolder.get().rollback(RootContext.getXID());
        } catch (TransactionException e) {
            log.error(e.getMessage(),e);
        }
        return Result.fail(440,"服务正在阻塞，请稍后再试(T_T)！！！");
    }
}
